#!/usr/bin/env bash

# Copyright (c) 2019 Tigera, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set -x

vm_name=$1
project=unique-caldron-775
zone=europe-west3-c

gcloud config set project $project
gcloud auth activate-service-account --key-file=$HOME/secrets/secret.google-service-account-key.json

function create-vm() {
  gcloud --quiet compute instances create "${vm_name}" \
           --zone=${zone} \
           --machine-type=n1-standard-4 \
           --image=ubuntu-2004-focal-v20230302 \
           --image-project=ubuntu-os-cloud \
           --boot-disk-size=20GB \
           --boot-disk-type=pd-standard && \
  for ssh_try in $(seq 1 10); do
    echo "Trying to SSH in: $ssh_try"
    gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- echo "Success" && break
    sleep 1
  done  && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- sudo apt install apt-transport-https ca-certificates curl software-properties-common && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --yes --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg" && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- "echo 'deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable' | sudo tee /etc/apt/sources.list.d/docker.list" && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- sudo apt update -y && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- sudo apt install -y --no-install-recommends git docker-ce=5:20.10.9~3-0~ubuntu-focal docker-ce-cli=5:20.10.9~3-0~ubuntu-focal containerd.io make iproute2 wireguard && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- sudo usermod -a -G docker ubuntu && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- sudo modprobe ipip && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- 'if [ -s /etc/docker/daemon.json ] ; then cat /etc/docker/daemon.json | sed "\$d" | sed "\$s/\$/,/" > /tmp/daemon.json ; else echo -en {\\n > /tmp/daemon.json ; fi' && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- 'cat >> /tmp/daemon.json << EOF
  "ipv6": true,
  "fixed-cidr-v6": "2001:db8:1::/64"
}
EOF' && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" --  sudo mv /tmp/daemon.json /etc/docker/daemon.json && \
  gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" --  sudo systemctl restart docker && \
  set +x && \
  echo "$DOCKERHUB_PASSWORD" | gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- docker login --username "$DOCKERHUB_USERNAME" --password-stdin && \
  set -x && \
  gcloud --quiet compute scp --zone=${zone} --recurse --compress "$(dirname $(pwd))" "ubuntu@${vm_name}:/home/ubuntu/calico"
}

function delete-vm() {
  gcloud --quiet compute instances delete "${vm_name}" --zone=${zone}
}

for attempt in $(seq 1 5); do
  echo "Trying to create text VM, attempt ${attempt}"
  if create-vm; then
    echo "Success!"
    exit 0
  else
    echo "Failed to create VM.  Tearing it down."
    delete-vm || true
  fi
done

echo "Out of retries"
exit 1
